using ASP;
using HoWave.Common;
using HoWave.Data;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Collections;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Admin_Admin_JXC_CalculateUnitManage : Admin, IRequiresSessionState
{
	protected ScriptManager sm;
	protected HtmlTable table_item;
	protected GridView gvList;
	protected Label PagePanel;
	protected Button btnMoreDelete;
	protected PlaceHolder phManage;
	protected Literal ltrTitle;
	protected TextBox txtCalculateUnitNumber;
	protected TextBox txtCalculateUnitName;
	protected TextBox txtRemark;
	protected HiddenField hfUnitName;
	protected Button btnAdd;
	protected HtmlTable AddTable;
	protected PlaceHolder phAddOrEdit;
	protected PlaceHolder phCheck;
	private string linkhead = "";
	private string beforeurl = "Admin_JXC_CalculateUnitManage.aspx?MenuID=1&MenuName=进销存管理&FileName=计量单位管理";
	private int infoID = 0;
	private string actionTab = "";
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	public int InfoID
	{
		get
		{
			if (base.Request.QueryString["InfoID"] != null)
			{
				this.infoID = Convert.ToInt32(base.Request.QueryString["InfoID"].Trim());
			}
			return this.infoID;
		}
	}
	public string ActionTab
	{
		get
		{
			this.actionTab = base.ActionTabInfo;
			return this.actionTab;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.admin.CheckAdminPower(46))
			{
				base.WriteLog("Modidy", Language.GetAdminMsg("Log/Access", "0"), "Admin_JXC_CalculateUnitManage");
				base.WriteRightErrMsg();
			}
			this.ShowPanelDisplay(base.ActionTabInfo);
			this.txtCalculateUnitNumber.Enabled = false;
			if (this.InfoID > 0)
			{
				this.InitPage();
			}
		}
	}
	private string GetInfoNum()
	{
		DataTable dataTable = BLLInstance.BllCalculateUnit.GetList("").Tables[0];
		string result;
		if (dataTable.Rows.Count > 0)
		{
			int num = dataTable.Rows.Count + 1001;
			string text = num.ToString();
			bool flag = false;
			foreach (DataRow dataRow in dataTable.Rows)
			{
				string a = dataRow["CalculateUnitNumber"].ToString();
				if (a == text)
				{
					flag = true;
					break;
				}
			}
			if (flag)
			{
				text = "";
				ArrayList arrayList = new ArrayList();
				for (int i = 1001; i <= num; i++)
				{
					string value = i.ToString();
					arrayList.Add(value);
				}
				foreach (DataRow dataRow in dataTable.Rows)
				{
					string a2 = dataRow["CalculateUnitNumber"].ToString();
					for (int i = 0; i < arrayList.Count; i++)
					{
						string text2 = arrayList[i].ToString();
						if (a2 == text2)
						{
							arrayList.Remove(text2);
							break;
						}
					}
				}
				if (arrayList.Count > 0)
				{
					text = arrayList[0].ToString();
				}
			}
			result = text;
		}
		else
		{
			result = "1001";
		}
		return result;
	}
	protected void ShowPanelDisplay(string DisplayName)
	{
		if (DisplayName != null)
		{
			if (DisplayName == "addinfo")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = true;
				this.phCheck.Visible = false;
				this.table_item.Visible = false;
				this.ltrTitle.Text = "添加计量单位信息";
				this.btnAdd.Text = "添加";
				this.txtCalculateUnitNumber.Text = this.GetInfoNum();
				return;
			}
			if (DisplayName == "editinfo")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = true;
				this.phCheck.Visible = false;
				this.table_item.Visible = false;
				this.ltrTitle.Text = "修改计量单位信息";
				this.btnAdd.Text = "修改";
				return;
			}
			if (DisplayName == "checkinfo")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = false;
				this.phCheck.Visible = true;
				this.table_item.Visible = false;
				this.btnAdd.Text = "查看计量单位信息";
				return;
			}
		}
		this.phManage.Visible = true;
		this.phAddOrEdit.Visible = false;
		this.phCheck.Visible = false;
		this.table_item.Visible = true;
		this.BindList();
	}
	protected void BindList()
	{
		string strWhere = this.QuestString();
		string orderby = "CalculateUnitID ASC";
		int num = 0;
		if (Fetch.Get("pageindex") != null)
		{
			num = Convert.ToInt32(Fetch.GetID("pageindex").ToString());
			if (num == 0)
			{
				num = 1;
			}
		}
		int pageSize = this.gvList.PageSize;
		int recordCount = BLLInstance.BllCalculateUnit.GetRecordCount(strWhere);
		int startIndex = (num - 1) * pageSize + 1;
		int endIndex = num * pageSize;
		DataTable dataSource = BLLInstance.BllCalculateUnit.GetListByPage(strWhere, orderby, startIndex, endIndex).Tables[0];
		this.gvList.DataSource = dataSource;
		this.gvList.DataBind();
		CurrPageLinks currPageLinks = new CurrPageLinks();
		string url = this.beforeurl + this.linkhead + "&pageindex";
		this.PagePanel.Text = currPageLinks.CurrPageLink(num, pageSize, recordCount, "条记录&nbsp;&nbsp;每页" + pageSize + "条数据&nbsp;&nbsp;", url);
	}
	protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E7EDFE'");
			e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
		}
	}
	protected string QuestString()
	{
		return " ";
	}
	protected void Button_Command(object sender, CommandEventArgs e)
	{
		string commandName = e.CommandName;
		if (commandName == "MoreDel")
		{
			string text = Text.CheckNumList(Fetch.GetOrPost("chk_select"));
			if (text != "")
			{
				bool flag = false;
				string[] array = text.Split(new char[]
				{
					','
				});
				for (int i = 0; i < array.Length; i++)
				{
					flag = this.delInfo(array[i]);
				}
				if (flag)
				{
					this.WriteErrMsg("", "批量信息删除成功！", this.beforeurl + this.linkhead + "&pageindex");
				}
			}
			else
			{
				this.WriteErrMsg("", "请选您要删除的信息", "");
			}
		}
		else
		{
			if (commandName == "DelInfo")
			{
				string id = e.CommandArgument.ToString();
				bool flag2 = this.delInfo(id);
				if (flag2)
				{
					this.WriteErrMsg("", "删除成功！", this.beforeurl + this.linkhead + "&pageindex");
				}
			}
		}
		this.BindList();
	}
	private bool delInfo(string id)
	{
		bool result = false;
		HoWave.Data.Model.CalculateUnit model = BLLInstance.BllCalculateUnit.GetModel(Convert.ToInt32(id));
		if (model != null)
		{
			result = BLLInstance.BllCalculateUnit.Delete(Convert.ToInt32(id));
		}
		return result;
	}
	protected void InitPage()
	{
		HoWave.Data.Model.CalculateUnit model = BLLInstance.BllCalculateUnit.GetModel(this.InfoID);
		if (model != null)
		{
			string actionTabInfo = base.ActionTabInfo;
			if (actionTabInfo != null)
			{
				if (!(actionTabInfo == "editinfo"))
				{
					if (!(actionTabInfo == "checkinfo"))
					{
					}
				}
				else
				{
					this.txtCalculateUnitNumber.Text = model.CalculateUnitNumber;
					this.txtCalculateUnitName.Text = model.CalculateUnitName;
					this.txtRemark.Text = model.Remark;
					this.hfUnitName.Value = model.CalculateUnitName;
				}
			}
		}
	}
	private HoWave.Data.Model.CalculateUnit GetInfoModel()
	{
		return new HoWave.Data.Model.CalculateUnit
		{
			CalculateUnitNumber = this.txtCalculateUnitNumber.Text,
			CalculateUnitName = this.txtCalculateUnitName.Text,
			Remark = this.txtRemark.Text
		};
	}
	private bool CheckInput()
	{
		bool flag = true;
		string msg = "";
		string text = this.txtCalculateUnitName.Text.Trim();
		if (!string.IsNullOrEmpty(text))
		{
			DataTable dataTable = BLLInstance.BllCalculateUnit.GetList("CalculateUnitName='" + text + "'").Tables[0];
			if (base.ActionTabInfo == "addinfo")
			{
				if (dataTable.Rows.Count > 0)
				{
					msg = "名称不能重复";
					flag = false;
				}
			}
			else
			{
				if (base.ActionTabInfo == "editinfo")
				{
					if (this.hfUnitName.Value != text)
					{
						if (dataTable.Rows.Count > 0)
						{
							msg = "名称不能重复";
							flag = false;
						}
					}
				}
			}
		}
		if (!flag)
		{
			MessageBox.Show(this, msg);
		}
		return flag;
	}
	protected void btnAdd_Click(object sender, EventArgs e)
	{
		if (this.CheckInput())
		{
			HoWave.Data.Model.CalculateUnit infoModel = this.GetInfoModel();
			string actionTabInfo = base.ActionTabInfo;
			if (actionTabInfo != null)
			{
				if (!(actionTabInfo == "addinfo"))
				{
					if (actionTabInfo == "editinfo")
					{
						HoWave.Data.Model.CalculateUnit calculateUnit = BLLInstance.BllCalculateUnit.GetModel(this.InfoID);
						calculateUnit = infoModel;
						calculateUnit.CalculateUnitID = this.InfoID;
						bool flag = BLLInstance.BllCalculateUnit.Update(calculateUnit);
						if (flag)
						{
							this.WriteSuccessMsg("", "信息修改成功！", this.beforeurl);
						}
						else
						{
							this.WriteErrMsg("", "信息修改失败！", "");
						}
					}
				}
				else
				{
					int num = BLLInstance.BllCalculateUnit.Add(infoModel);
					if (num > 0)
					{
						this.WriteSuccessMsg("", "信息添加成功！", this.beforeurl);
					}
					else
					{
						this.WriteErrMsg("", "信息添加失败！", "");
					}
				}
			}
		}
	}
}
